package com.hbmcc.rxjava.test

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.hbmcc.rxjava.R
import com.hbmcc.rxjava.annotation.MethodInfo
import com.hbmcc.rxjava.base.TitleActivity
import com.hbmcc.rxjava.config.INTENT_FILE_PATH
import com.hbmcc.rxjava.config.INTENT_FILE_PREFIX
import com.hbmcc.rxjava.config.INTENT_TITLE
import java.util.*
import kotlin.collections.ArrayList

/**
 * 创建日期：2020/11/2
 * 描述:所有知识点的类
 * 作者:支豪亮
 */

const val METHOD_NODE = "NONE"
abstract class TestActivity : TitleActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        val filePrefix = intent.getStringExtra(INTENT_FILE_PREFIX)?:""
        val list = resources.assets.list(filePrefix)?.toList() ?: ArrayList()
        val listMethod = getAllMethod(list)

        val titleList = getTestTitle()
        for (titleIndex in titleList.indices) {
            listMethod.add(
                MethodBean(
                    titleIndex + 1,
                    0,
                    titleList[titleIndex],
                    METHOD_NODE,
                    R.layout.view_test_title,
                    list.contains("${titleIndex + 1}.0.html")
                )
            )
        }
        listMethod.sortBy { item -> (item.textIndex shl 8) + item.order }
        initView(listMethod, filePrefix)
    }


    abstract fun getTestTitle(): List<String>

    private fun initView(list: MutableList<MethodBean>, filePrefix: String) {
        val recyclerView: RecyclerView = findViewById(R.id.rclTest)
        val layoutManager = VirtualLayoutManager(this)
        recyclerView.layoutManager = layoutManager


        val delegateAdapter = DelegateAdapter(layoutManager, true)
        recyclerView.adapter = delegateAdapter

        val viewPool = RecyclerView.RecycledViewPool()
        recyclerView.setRecycledViewPool(viewPool)

        val adapters: MutableList<DelegateAdapter.Adapter<*>> = LinkedList()
        adapters.add(ProxyTestAdapter(list, object : IMethodCallback {
            override fun onClick(methodBean: MethodBean) {
                invokeMethod(methodBean)
            }

            override fun onInfo(methodBean: MethodBean) {
                goFile(methodBean,filePrefix)
            }

        }))
        delegateAdapter.setAdapters(adapters)
    }

    private fun invokeMethod(methodBean: MethodBean){
        if(METHOD_NODE == methodBean.methodName){
            return
        }
        val method = this::class.java.getMethod(methodBean.methodName)
        method.invoke(this)

    }


    private fun goFile(methodBean: MethodBean,filePrefix:String) {
        if (methodBean.isShowInfo) {
            val intent = Intent(this, FileBrowserActivity::class.java)
            intent.putExtra(INTENT_TITLE, methodBean.title)
            intent.putExtra(INTENT_FILE_PATH, "$filePrefix/${methodBean.textIndex}.${methodBean.order}.html")
            startActivity(intent)
        }
    }

    private fun getAllMethod(list: List<String>): MutableList<MethodBean> {
        val methodAll = this.javaClass.declaredMethods
        val listMethod: MutableList<MethodBean> = ArrayList()
        for (method in methodAll) {
            val hasAnnotation: Boolean = method.isAnnotationPresent(MethodInfo::class.java)
            if (!hasAnnotation) continue
            val methodInfo = method.getAnnotation(MethodInfo::class.java) ?: continue
            listMethod.add(
                MethodBean(
                    methodInfo.textIndex,
                    methodInfo.order,
                    methodInfo.title,
                    method.name,
                    R.layout.view_test_item,
                    list.contains("${methodInfo.textIndex}.${methodInfo.order}.html")
                )
            )

        }
        return listMethod
    }

}
